package com.runner;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.db.Dao;
import com.db.DaoImpl;
import com.loader.ConfigLoader;
import com.utils.DdmapProcessor;
import com.utils.Processor;
import com.utils.StringTools;
import com.utils.UrlExtractor;

public class DdmapSite extends Site{
	
	private Set<String> listUrlSet = new HashSet<String>();
	private Set<String> detailUrlSet = new HashSet<String>();
	
	public DdmapSite(Map<String, String> map,int parseType) {
		super(map,parseType);
	}

	class ParseContent implements Runnable {
		String entrance = "";
		int type = 0;
		public ParseContent(String entrance, int type) {
			this.entrance = entrance;
			this.type = type;
		}
		
		@Override
		public void run() {
			parseList(entrance,parseType);
		}
		
	} 
	
	public static void main(String[] args){
		ConfigLoader loader = new ConfigLoader();
		Map<String, Map<String, String>> sitesMap = loader.loadConfigInfo("files/BBSFilter.xml");
		new DdmapSite(sitesMap.get("1"),0).parseDetail("http://tuan.ddmap.com/105",0);
	}
	
	@Override
	public void run() {
		
		//获取今天的团购信息,parseType为0
		String[] entranceList = getEntrance("TodayEntrance");
		for(String entrance: entranceList){
			new Thread(new ParseContent(entrance,parseType)).start();
		}
		
//		//获取以往的团购信息,parseType为1
//		String[] entranceList1 = getEntrance("LastEntrance");
//		for(String entrance: entranceList1){
//			new Thread(new ParseContent(entrance,1)).start();
//		}
	}
	
	//根据传入的字符串在配置文件中进行匹配，获取相应的value，例如：
	//传入TodayEntrance则或者的是对应的今天的团购信息
	private String[] getEntrance(String entrance){
		return map.get(entrance).split(",");
	}
	
	@Override
	public void parseList(String link, int parseType) {
		if (listUrlSet.contains(link)) {
			return;
		}	
		listUrlSet.add(link);
		
		System.out.println(link+"1111111111111111111111111111111111");
		
		String content = Processor.getContent(link);
		Set<String> urlsSet = UrlExtractor.getURLs(content, link);
		if (parseType == 2) {
			parseDetail(link, parseType);
			return;
		}
		
		//遍历今日和历史的团购，主要区别是可能存入不同的表或者sql不一样，所以使用了if语句
		if (parseType ==1) {
//			String[] lastEntrances = map.get("LastEntrance").split(",");
			//TODO
		}else {
			//如果是下一页则循环调用
			String[] isTodayNextPageFilter = map.get("TodayNextPage").split(",");
			String[] detailPageFilter = map.get("DetailPage").split(",");
			boolean isNextPage;
			boolean isDetailPage;
			for(String url: urlsSet){
				isNextPage = StringTools.isExistIn(isTodayNextPageFilter, url);
				if (isNextPage) {
					parseList(url, parseType);
					System.out.println(link+"2222222222222222222222222222222");
					continue;
				}
				isDetailPage = StringTools.isExistIn(detailPageFilter, url);
				if (isDetailPage) {
					parseDetail(url, parseType);
				}
			}
		}
		
	}
	//此函数实现的是元数据的提取
	@Override
	public void parseDetail(String url,int parseType) {
		if(detailUrlSet.contains(url)){
			return;
		}
		detailUrlSet.add(url);
		
		System.out.println(url+"333333333333333333333333333333333333333");
		
		Processor processor = new DdmapProcessor();
		String regexString = map.get("StringCutFlag");
		String cutStartString = regexString.substring(0, regexString.indexOf("("));
		String cutEndString = regexString.substring(regexString.indexOf(")")+1);
		//cutString为截取了的字符串，此字符串中只包含评论的相关数据
		String cutString = processor.getContentByCon(url, cutStartString, cutEndString);
		
		//下面的是对每个用户的发言的遍历
		String oneCommentRegex = map.get("CommentContent");
		String[] comments = Processor.getAllValue(cutString, oneCommentRegex);
		//没有回帖，直接返回
		if ((comments==null)||(comments.length==0)) {
			return;
		}
		for(String comment:comments){
//			String commentUserName = Processor.getValue(comment, map.get("CommentUser"));
			String commentUserName = processor.getValueByCon(comment,map.get("CommentUser"),"2");
			String commentValue = processor.getValueByCon(comment,map.get("CommentValue"),"1");
			
			//这里过滤有回帖的，即暂时不处理回帖
			if(commentValue.equals("")){
				//TODO
				continue;
//				System.out.println("---------有引用的回帖----------");
//				String quoteValue = Processor.getValue(comment, map.get("QuoteValue"));
//				System.out.println(quoteValue);
//				commentValue = getAnotherCommend(comment,map.get("AnotherCommentValue"));
			}
			
			String commentTime = Processor.getValue(comment, map.get("CommentTime"));
			
			System.out.println(commentUserName);
			System.out.println(commentValue);
			System.out.println(commentTime);
			
			//下面执行数据库的插入操作
//			String insertSql = "";
//			doDbInsert(insertSql);
		}

	}

	private void doDbInsert(String sql){
		Dao dbDao = new DaoImpl();
		dbDao.insert(sql);
	}

	private String getAnotherCommend(String commentConent, String regex){
		String result = "";
		result = Processor.getValue(commentConent, regex);
		return result;
	}
}